package com.kc.sjwh.other;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询条件(配合_commonMapper.xml使用)
 * date: 2017年5月17日
 * @author zhangGuihua(hua7381@163.com)
 */
public class QueryParam {
	private List<QueryCondition> conditions = new ArrayList<QueryCondition>();
	private List<String> sqlConditions = new ArrayList<String>();
	private Map map = new HashMap();//额外参数
	private int pageSize;//分页大小
	private int pageNum;//页码
	private int begin;
	private int end;
	private String order;
	public QueryParam() {
		
	}
	public QueryParam(int ps, int pn) {
		this.pageSize = ps;
		this.pageNum = pn;
	}
	public void figurePage(int total) {
		int maxPn = (total - 1) / pageSize + 1;
		if(pageNum > maxPn) pageNum = maxPn;
		begin = pageSize*(pageNum-1)+1;
		end = pageSize*pageNum;
	}
	
	public QueryParam addCondition(String property, String operation, Object value) {
		
		if (!property.contains(".") && !"rownum".equals(property)) {
			property = "t." + property;
		}
		
		/*数据库为oracle时启用*/
		if(value instanceof java.util.Date) {//oracle 中，为了能触发索引  date类型特殊处理
			java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			this.addSqlCondition(String.format("%s %s to_date('%s', 'yyyy-mm-dd hh24:mi:ss')", 
					property, operation, fmt.format((java.util.Date)value)));
			return this;
		}
		
		conditions.add(new QueryCondition(property, operation, value));
		return this;
	}
	
	public QueryParam addSqlCondition(String sqlCondition) {
		this.sqlConditions.add(sqlCondition);
		return this;
	}
	
	public QueryParam put(String property, Object value) {
		return addCondition(property, "=", value);
	}

	//getters and setters
	public List<QueryCondition> getConditions() {
		return conditions;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public void setConditions(List<QueryCondition> conditions) {
		this.conditions = conditions;
	}
	public List<String> getSqlConditions() {
		return sqlConditions;
	}
	public void setSqlConditions(List<String> sqlConditions) {
		this.sqlConditions = sqlConditions;
	}
	public int getBegin() {
		return begin;
	}
	public void setBegin(int begin) {
		this.begin = begin;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public String getOrder() {
		return order;
	}
	public QueryParam setOrder(String order) {
		this.order = order;
		return this;
	}

	@SuppressWarnings("unused")
	private class QueryCondition {
		private String property;
		private String operation;
		private Object value;

		public QueryCondition(String property, String operation, Object value) {
			this.property = property;
			this.operation = operation;
			this.value = value;
		}

		public String getProperty() {
			return property;
		}
		public void setProperty(String property) {
			this.property = property;
		}
		public String getOperation() {
			return operation;
		}
		public void setOperation(String operation) {
			this.operation = operation;
		}
		public Object getValue() {
			return value;
		}
		public void setValue(Object value) {
			this.value = value;
		}
	}

	/**
	 * 往map中添加
	 * @param key
	 * @param val
	 * @return
	 */
	public QueryParam putToMap(String key, String val) {
		map.put(key, val);
		return this;
	}
}

